package com.gzhryc.html;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gzhryc.jdbc.MultiDBTools;
import org.apache.commons.lang3.StringUtils;

import com.gzhryc.html.dao.WebsitePageDao;
import com.gzhryc.html.dao.db.WebsitePage;
import com.gzhryc.html.dto.WebsitePageSearch;
import com.gzhryc.jdbc.Conditions;

public class WebsitePageService {

	public static WebsitePageService self() {
		return MultiDBTools.getService(WebsitePageService.class, true);
	}

	public static WebsitePageService self(String jdbcKey) {
		return MultiDBTools.getService(jdbcKey,WebsitePageService.class, true);
	}


	WebsitePageDao websitePageDao;
	
	public WebsitePageService() {
		this.websitePageDao = new WebsitePageDao(null);
	}

	public WebsitePageService(String jdbcKey) {
		this.websitePageDao = new WebsitePageDao(jdbcKey);
	}
	
	public List<WebsitePage> findBySearch(WebsitePageSearch search,Integer pageNo,Integer pageLimit){
		Conditions condition = websitePageDao.buildConditions(search);
		condition.asc("sort");
		try {
			return websitePageDao.findByCondition(condition,pageNo,pageLimit);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public Long countBySearch(WebsitePageSearch search) {
		Conditions condition = websitePageDao.buildConditions(search);
		try {
			return websitePageDao.count(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return 0L;
	}
	
	public List<WebsitePage> findByWebsiteId(Long websiteId){
		Conditions condition = new Conditions(WebsitePage.class);
		condition.and().is("websiteId", websiteId);
		condition.asc("sort");
		try {
			return websitePageDao.findByCondition(condition);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public WebsitePage getById(Long id) {
		try {
			return websitePageDao.getById(id);
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public WebsitePage getByWebsiteIdAndPath(Long websiteId,String path){
		if(websiteId != null && StringUtils.isNotBlank(path)){
			Conditions condition = new Conditions(WebsitePage.class);
			condition.and().is("websiteId", websiteId);
			condition.and().is("path", path);
			try {
				return websitePageDao.get(condition);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return null;
	}
	
	public List<WebsitePage> sort(List<WebsitePage> data,Long parentId){
		List<WebsitePage> result = null;
		if(data != null){
			result = new ArrayList<WebsitePage>(data.size());
			for(WebsitePage websitePage : data){
				if(websitePage.getParentId().equals(parentId)){
					result.add(websitePage);
					result.addAll(sort(data,websitePage.getId()));
				}
			}
		}
		return result;
	}
	
	public boolean save(WebsitePage websitePage) {
		if(websitePage.getId() != null) {
			try {
				return websitePageDao.updateNotNull(websitePage) > 0;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}else {
			if(websitePage.getSort() == null) {
				websitePage.setSort(10);
			}
			try {
				return websitePageDao.insert(websitePage) > 0;
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
		return false;
	}
	
	public boolean updateBanners(Long id,String banners) {
		Map<String,Object> data = new HashMap<String,Object>();
		data.put("id", id);
		data.put("banners", banners);
		try {
			return websitePageDao.update(data) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	public boolean deleteById(Long id) {
		try {
			return websitePageDao.deleteById(id) > 0;
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return false;
	}
}
